package GUI;

import javax.swing.JOptionPane;

import Kernel.Administrateur;
import Kernel.Ville;

public class EditerVille extends javax.swing.JFrame {

    private static final long serialVersionUID = 8963751007569430924L;
	
	/** Cree une nouvelle instance de EditerVille */
    public EditerVille() {
        initComponents();
        this.setLocationRelativeTo(this.getParent());
    }

    /**
	 * Initialisation des composants d'AWT.
	 */
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        motifRecherche = new javax.swing.JTextField();
        rechercher = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        nomVille = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        nombreHabitants = new javax.swing.JTextField();
        asAeroport = new javax.swing.JCheckBox();
        asGare = new javax.swing.JCheckBox();
        asGareRoutiere = new javax.swing.JCheckBox();
        asPort = new javax.swing.JCheckBox();
        editer = new javax.swing.JButton();
        retour = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Editer une ville");

        jLabel2.setText("Rechercher par nom");

        rechercher.setText("Rechercher");
        rechercher.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rechercherActionPerformed(evt);
            }
        });

        jLabel3.setText("Modifier la ville");

        jLabel4.setText("Nom");

        jLabel5.setText("Habitants");

        asAeroport.setText("Aeroport");

        asGare.setText("Gare");

        asGareRoutiere.setText("Gare Routiere");

        asPort.setText("Port");

        editer.setText("Editer");
        editer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editerActionPerformed(evt);
            }
        });

        retour.setText("Retour");
        retour.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                retourActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(motifRecherche, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(59, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(335, Short.MAX_VALUE)
                .addComponent(rechercher)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(163, 163, 163)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(218, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(144, 144, 144)
                .addComponent(jLabel1)
                .addContainerGap(194, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(142, 142, 142)
                        .addComponent(jLabel3)
                        .addGap(45, 45, 45))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel4))
                        .addGap(20, 20, 20)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(nomVille, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(nombreHabitants, javax.swing.GroupLayout.DEFAULT_SIZE, 153, Short.MAX_VALUE)
                                .addGap(34, 34, 34)))))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(asPort)
                    .addComponent(asGareRoutiere)
                    .addComponent(asAeroport)
                    .addComponent(asGare))
                .addContainerGap(38, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(retour)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 304, Short.MAX_VALUE)
                .addComponent(editer)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(motifRecherche, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rechercher)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(asAeroport)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(asGare)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(asGareRoutiere))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(nomVille, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addGap(14, 14, 14)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(nombreHabitants, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(asPort)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(editer)
                    .addComponent(retour))
                .addContainerGap())
        );

        pack();
    }

    private void rechercherActionPerformed(java.awt.event.ActionEvent evt) {
    	Ville v;
    	v = Accueil.serveur.chercherVille(this.motifRecherche.getText());
    	if (v!=null){
    		this.nomVille.setText(""+v.getNom());
    		this.nombreHabitants.setText(""+v.getPopulation()); 
    		this.asAeroport.setSelected(v.isExisteAeroport());
    		this.asGare.setSelected(v.isExisteGare());
    		this.asGareRoutiere.setSelected(v.isExisteGareRoutiere());
    		this.asPort.setSelected(v.isExistePort());
    	}
    	else{
    		JOptionPane.showMessageDialog(this,
					"La reference n'a pu etre trouvee !", "Pas glop...",
					JOptionPane.ERROR_MESSAGE);
    	}
    }

    private void retourActionPerformed(java.awt.event.ActionEvent evt) {
    	Administration at = new Administration(new Administrateur(Administration.admin.getLogin(), Administration.admin.getPassword(), Accueil.serveur));
		at.setVisible(true);
		this.setVisible(false);
		this.dispose();
    }

    private void editerActionPerformed(java.awt.event.ActionEvent evt) {
    	if(this.nomVille.getText().compareTo("")==0 || this.nombreHabitants.getText().compareTo("")==0)
    		JOptionPane.showMessageDialog(this,
					"Les champs ne peuvent etre vide !", "Pas glop...",
					JOptionPane.ERROR_MESSAGE);
    	Ville v = new Ville(this.nomVille.getText(),Integer.parseInt(this.nombreHabitants.getText()),this.asAeroport.isSelected(),this.asGare.isSelected(),this.asGareRoutiere.isSelected(),this.asPort.isSelected());
    	Accueil.serveur.supprimerVille(Accueil.serveur.chercherVille(this.motifRecherche.getText()));
    	Accueil.serveur.ajouterVille(v);
    	this.setVisible(false);    	
		Administration ad = new Administration(new Administrateur(Administration.admin.getLogin(), Administration.admin.getPassword(), Accueil.serveur));
		ad.setVisible(true);
		this.dispose();
    }

    private javax.swing.JCheckBox asAeroport;
    private javax.swing.JCheckBox asGare;
    private javax.swing.JCheckBox asGareRoutiere;
    private javax.swing.JCheckBox asPort;
    private javax.swing.JButton editer;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField motifRecherche;
    private javax.swing.JTextField nomVille;
    private javax.swing.JTextField nombreHabitants;
    private javax.swing.JButton rechercher;
    private javax.swing.JButton retour;

}
